import allure
from appium.webdriver.common.appiumby import AppiumBy

from .base import Base


class AddMember(Base):
    # 手动输入添加
    __MANUAL_INPUT_ADD = (AppiumBy.XPATH, '//android.widget.TextView[@text="手动输入添加"]')
    # 快速输入
    __QUICK_INPUT = (AppiumBy.XPATH, '//*[@text="快速输入"]')
    # 完整输入
    __FULL_INPUT = (AppiumBy.XPATH, '//*[@text="完整输入"]')
    # 完整输入->快速输入 报错弹窗
    __FULL_TO_QUICK = (AppiumBy.XPATH, '//*[@text="确定"]')
    # 姓名
    __NAME = (AppiumBy.XPATH, '//*[@text="姓名"]/../*[3]')
    # 选择手机号
    __SELECT_PHONE = (AppiumBy.XPATH, '//*[@text="手机"]/../*[2]/*[1]')
    # 选择手机号->查询
    __SELECT_PHONE_CHECK = (AppiumBy.XPATH, '//*[@text="选择国家和地区"]/../../../../../*[2]')
    # 选择手机号->查询->输入
    __SELECT_PHONE_CHECK_INPUT = (AppiumBy.XPATH, '//*[@text="搜索"]')
    # 手机号
    __PHONE_NUMBER = (AppiumBy.XPATH, '//*[@text="手机"]/../*[2]/*[2]/*[1]')
    # 邮箱
    __MAIL = (AppiumBy.XPATH, '//*[@text="邮箱"]/../*[2]')
    # 邀请
    __INVITE = (AppiumBy.XPATH, '//*[@text="保存后自动发送邀请通知"]')
    # 保存
    __SAVE = (AppiumBy.XPATH, '//*[@text="保存"]')
    # 保存继续添加
    __SAVE_CONTINUE_ADD = (AppiumBy.XPATH, '//*[@text="保存并继续添加"]')
    # 添加成功的 Toast 弹窗
    __SAVE_TOAST = (AppiumBy.XPATH, "//*[contains(@text, '添加成功')]")
    # 添加失败的弹窗的文字
    __SAVE_FAIL = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[2]/*[@text="确定"]/../../*[1]/*/*')
    # 添加失败的弹窗的确定
    __SAVE_FAIL_CONFIRM = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[2]/*[@text="确定"]')
    # 账号
    __ACCOUNT = (AppiumBy.XPATH, '//*[@text="账号"]/../*[3]')
    # 别名
    __ALIAS = (AppiumBy.XPATH, '//*[@text="别名"]/../*[2]')
    # 性别
    __GENDER = (AppiumBy.XPATH, '//*[@text="性别"]/../*[2]')
    # 性别->男
    __GENDER_MAN = (AppiumBy.XPATH, '//*[@text="男"]')
    # 性别->女
    __GENDER_GRIL = (AppiumBy.XPATH, '//*[@text="女"]')
    # 企业邮箱
    __ENTERPRISE_MAILBOX = (AppiumBy.XPATH, '//*[@text="企业邮箱"]/../*[2]')
    # 座机
    __TELEPHONE = (AppiumBy.XPATH, '//*[@text="座机"]/../*[2]')
    # 地址
    __ADDRESS = (AppiumBy.XPATH, '//*[@text="地址"]/../*[2]')
    # 地址->输入框
    __ADDRESS_INPUT = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[2]/*[1]')
    # 地址->确定
    __ADDRESS_CONFIRM = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[1]//*[@text="确定"]')
    # 职务
    __POST = (AppiumBy.XPATH, '//*[@text="职务"]/../*[2]')
    # 部门负责人
    __DEPARTMENT_HEAD = (AppiumBy.XPATH, '//*[@text="部门负责人"]/../../../*[2]/*')
    # 直属上级
    __DIRECT_SUPERVISOR = (AppiumBy.XPATH, '//*[@text="直属上级"]/../*[2]')
    # 直属上级->查询
    __DIRECT_SUPERVISOR_CHECK = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*/*/*[1]/*[2]')
    # 直属上级->输入框
    __DIRECT_SUPERVISOR_INPUT = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*/*/*/*[2]/*')
    # 直属上级->确定
    __DIRECT_SUPERVISOR_CONFIRM = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[2]/*/*/*[2]')
    # 对外职务
    __EXTERNAL_DUTIES = (AppiumBy.XPATH, '//*[@text="对外职务"]/../*[2]')
    # 对外职务->设置
    __EXTERNAL_DUTIES_SET = (AppiumBy.XPATH, '//*[@text="设置专属对外职务"]')
    # 对外职务->输入框
    __EXTERNAL_DUTIES_INPUT = (AppiumBy.XPATH, '//*[@text="设置专属对外职务"]/../../*[4]/*[1]')
    # 对外职务->确定
    __EXTERNAL_DUTIES_CONFIRM = (AppiumBy.XPATH, '//*[@resource-id="android:id/content"]/*/*[1]//*[@text="确定"]')

    def go_manual_input_add(self):  # 手动输入添加
        while allure.step("手动添加"):
            self.find_click(self.__MANUAL_INPUT_ADD)
            return AddMember(self.driver)

    def quick_input(self, item, continue_add=False):
        self.quick_full(op='quick')
        with allure.step("快速输入"):
            # 名字
            if item["name"]:
                self.find_clear(self.__NAME)
                self.find_key(self.__NAME, item["name"])
            # 选择手机号
            if item["select_phone"]:
                self.select_phone(item["select_phone"])
            # 手机号
            if item["phone_number"]:
                self.find_clear(self.__PHONE_NUMBER)
                self.find_key(self.__PHONE_NUMBER, item["phone_number"])
            # 邮箱
            if item["mail"]:
                self.find_clear(self.__MAIL)
                self.find_key(self.__MAIL, item["mail"])
            # 是否邀请
            if not item["invite"]:
                self.find_click(self.__INVITE)
            if item["img_name"]:
                self.img_log(item["img_name"], "输入")
            # 保存
            if not continue_add:
                self.find_click(self.__SAVE)
                self.implicitly_wait(1)
                if self.find(self.__SAVE_FAIL):  # 报错弹窗
                    text = self.find_text(self.__SAVE_FAIL)
                    if item["img_name"]:
                        self.img_log(f'{item["img_name"]}_fail', "报错")
                    return text
                text = self.find_text(self.__SAVE_TOAST)  # 添加成功
                if item["img_name"]:
                    self.img_log(f'{item["img_name"]}_toast', "成功")
                return text
            else:  # 继续添加
                self.find_click(self.__SAVE_CONTINUE_ADD)
                self.implicitly_wait(1)
                if self.find(self.__SAVE_TOAST) and item["img_name"]:
                    self.img_log(f'{item["img_name"]}_toast', "成功")
                elif self.find(self.__SAVE_FAIL):  # 报错弹窗
                    text = self.find_text(self.__SAVE_FAIL)
                    if item["img_name"]:
                        self.img_log(f'{item["img_name"]}_fail', "报错")
                    if item["ignore_fail"]:     # 忽略报错弹窗
                        self.implicitly_wait(10)
                        self.find_click(self.__SAVE_FAIL_CONFIRM)
                    else:
                        return text
                # 恢复默认的邀请
                if not item["invite"]:
                    self.find_click(self.__INVITE)

    def full_input(self, item, continue_add=False):
        self.quick_full(op='full')
        with allure.step("完整输入"):
            # 名字
            if item["name"]:
                self.swip_by(self.__NAME, operator="clear")
                self.find_key(self.__NAME, item["name"])
            # 账号
            if item["account"]:
                self.swip_by(self.__ACCOUNT, operator="clear")
                self.find_key(self.__ACCOUNT, item["account"])
            # 别名
            if item["alias"]:
                self.swip_by(self.__ALIAS, operator="clear")
                self.find_key(self.__ALIAS, item["alias"])
            # 性别
            if item["gender"] == "男":
                self.swip_by(self.__GENDER, operator="click")
                self.find_click(self.__GENDER_MAN)
            else:
                self.swip_by(self.__GENDER, operator="click")
                self.find_click(self.__GENDER_GRIL)
            # 企业邮箱
            if item["enterprise_mailbox"]:
                self.swip_by(self.__ENTERPRISE_MAILBOX, operator="clear")
                self.find_key(self.__ENTERPRISE_MAILBOX, item["enterprise_mailbox"])
            # 选择手机号
            if item["select_phone"]:
                self.select_phone(item["select_phone"])
            # 手机号
            if item["phone_number"]:
                self.swip_by(self.__PHONE_NUMBER, operator="clear")
                self.find_key(self.__PHONE_NUMBER, item["phone_number"])
            # 座机
            if item["telephone"]:
                self.swip_by(self.__TELEPHONE, operator="clear")
                self.find_key(self.__TELEPHONE, item["telephone"])
            # 邮箱
            if item["mail"]:
                self.swip_by(self.__MAIL, operator="clear")
                self.find_key(self.__MAIL, item["mail"])
            # 地址
            if item["address"]:
                self.swip_by(self.__ADDRESS, operator="click")
                self.find_clear(self.__ADDRESS_INPUT)
                self.find_key(self.__ADDRESS_INPUT, item["address"])
                self.find_click(self.__ADDRESS_CONFIRM)
            # 职务
            if item["post"]:
                self.swip_by(self.__POST, operator="clear")
                self.find_key(self.__POST, item["post"])
            # 部门负责人
            if item["department_head"]:
                self.swip_by(self.__DEPARTMENT_HEAD, operator="click")
            # 直属上级
            if item["direct_supervisor"]:
                self.swip_by(self.__DIRECT_SUPERVISOR, operator="click")
                self.find_click(self.__DIRECT_SUPERVISOR_CHECK)
                self.find_key(self.__DIRECT_SUPERVISOR_INPUT, item["direct_supervisor"])
                __SUPERVISOR = (AppiumBy.XPATH, f'//android.widget.TextView[@text="{item["direct_supervisor"]}"]')
                self.find_click(__SUPERVISOR)
                self.find_click(self.__DIRECT_SUPERVISOR_CONFIRM)
            # 对外职务
            if item["external_duties"]:
                self.swip_by(self.__EXTERNAL_DUTIES, operator="click")
                self.find_click(self.__EXTERNAL_DUTIES_SET)
                self.find_clear(self.__EXTERNAL_DUTIES_INPUT)
                self.find_key(self.__EXTERNAL_DUTIES_INPUT, item["external_duties"])
                self.find_click(self.__EXTERNAL_DUTIES_CONFIRM)
            # 邀请
            if not item["invite"]:
                self.swip_by(self.__INVITE, operator="click")
            # 保存
            if not continue_add:
                self.swip_by(self.__SAVE, operator="click")
                self.implicitly_wait(1)
                if self.find(self.__SAVE_FAIL):  # 报错弹窗
                    text = self.find_text(self.__SAVE_FAIL)
                    if item["img_name"]:
                        self.img_log(f'{item["img_name"]}_fail', "报错")
                    return text
                text = self.find_text(self.__SAVE_TOAST)  # 添加成功
                if item["img_name"]:
                    self.img_log(f'{item["img_name"]}_toast', "成功")
                return text
            else:  # 继续添加
                self.swip_by(self.__SAVE_CONTINUE_ADD, operator="click")
                self.implicitly_wait(1)
                if self.find(self.__SAVE_TOAST) and item["img_name"]:
                    self.img_log(f'{item["img_name"]}_toast', "成功")
                if self.find(self.__SAVE_FAIL):  # 报错弹窗
                    if item["img_name"]:
                        self.img_log(f'{item["img_name"]}_fail', "报错")
                    if item["ignore_fail"]:     # 忽略报错弹窗
                        self.implicitly_wait(10)
                        self.find_click(self.__SAVE_FAIL_CONFIRM)
                    else:
                        text = self.find_text(self.__SAVE_FAIL)
                        return text

    def repeatedly(self, repeatedly):
        with allure.step('多次输入'):
            for index, item in enumerate(repeatedly):
                # 前面为True,最后一次为False
                continue_add = (index != len(repeatedly) - 1)
                # 去添加
                if item["op"] == "quick":
                    text = self.quick_input(item, continue_add)
                else:
                    text = self.full_input(item, continue_add)
                if text:    # 如果有返回信息,则返回
                    return text

    def quick_full(self, op):   # 快速or完整
        self.implicitly_wait(1)
        if op == "quick":   # 快速
            if self.find(self.__QUICK_INPUT):
                self.find_click(self.__QUICK_INPUT)
                if self.find(self.__FULL_TO_QUICK):     # 切换到快速输入弹窗
                    self.find_click(self.__FULL_TO_QUICK)
        if op == "full":    # 完整
            if self.find(self.__FULL_INPUT):
                self.find_click(self.__FULL_INPUT)

    def select_phone(self, select_phone):   # 选择手机号
        if select_phone.isdigit():
            __CHECK = (AppiumBy.XPATH, f'//android.widget.ListView[@resource-id="com.tencent.wework:id/cro"]/*/*[2][@text="+{select_phone}"]')
        else:
            __CHECK = (AppiumBy.XPATH, f'//android.widget.ListView[@resource-id="com.tencent.wework:id/cro"]/*/*[1][@text="{select_phone}"]')

        self.swip_by(self.__SELECT_PHONE, operator="click")
        self.find_click(self.__SELECT_PHONE_CHECK)
        self.find_key(self.__SELECT_PHONE_CHECK_INPUT, select_phone)
        if self.find(__CHECK):
            self.find_click(__CHECK)
            return True
        return False
